package net.lab1024.sa.admin.module.business.cutting.service;

import java.util.List;
import net.lab1024.sa.admin.module.business.cutting.dao.CuttingInfoDao;
import net.lab1024.sa.admin.module.business.cutting.domain.entity.CuttingInfoEntity;
import net.lab1024.sa.admin.module.business.cutting.domain.form.CuttingInfoAddForm;
import net.lab1024.sa.admin.module.business.cutting.domain.form.CuttingInfoQueryForm;
import net.lab1024.sa.admin.module.business.cutting.domain.form.CuttingInfoUpdateForm;
import net.lab1024.sa.admin.module.business.cutting.domain.vo.CuttingInfoVO;
import net.lab1024.sa.base.common.code.UserErrorCode;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import net.lab1024.sa.base.module.support.datatracer.constant.DataTracerTypeEnum;
import net.lab1024.sa.base.module.support.datatracer.service.DataTracerService;
import net.lab1024.sa.base.module.support.serialnumber.constant.SerialNumberIdEnum;
import net.lab1024.sa.base.module.support.serialnumber.service.SerialNumberService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;

/**
 * 下料表 Service
 *
 * @Author zzx
 * @Date 2025-08-08 19:37:29
 * @Copyright zzx
 */

@Service
public class CuttingInfoService {

    @Resource
    private CuttingInfoDao cuttingInfoDao;
    @Resource
    private DataTracerService dataTracerService;
    @Resource
    private SerialNumberService serialNumberService;

    /**
     * 分页查询
     */
    public PageResult<CuttingInfoVO> queryPage(CuttingInfoQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<CuttingInfoVO> list = cuttingInfoDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(CuttingInfoAddForm addForm) {
        CuttingInfoEntity cuttingInfoEntity = SmartBeanUtil.copy(addForm, CuttingInfoEntity.class);
        cuttingInfoEntity.setDisabledFlag(0);
        Long userId = SmartRequestUtil.getRequestUserId();
        cuttingInfoEntity.setCreateUserId(userId);
        cuttingInfoEntity.setUpdateUserId(userId);
        cuttingInfoEntity.setCuttingDateId(serialNumberService.generate(SerialNumberIdEnum.CUTTING));
        cuttingInfoDao.insert(cuttingInfoEntity);
        dataTracerService.insert(cuttingInfoEntity.getCuttingId(), DataTracerTypeEnum.CUTTING);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     */
    public ResponseDTO<String> update(CuttingInfoUpdateForm updateForm) {
        CuttingInfoEntity oldEntity = cuttingInfoDao.selectById(updateForm.getCuttingId());
        CuttingInfoEntity cuttingInfoEntity = SmartBeanUtil.copy(updateForm, CuttingInfoEntity.class);
        Long userId = SmartRequestUtil.getRequestUserId();
        cuttingInfoEntity.setUpdateUserId(userId);
        cuttingInfoDao.updateById(cuttingInfoEntity);
        dataTracerService.update(cuttingInfoEntity.getCuttingId(), DataTracerTypeEnum.CUTTING, oldEntity, cuttingInfoEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        cuttingInfoDao.batchUpdateDeleted(idList, true);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Long cuttingId) {
        if (null == cuttingId){
            return ResponseDTO.ok();
        }

        cuttingInfoDao.updateDeleted(cuttingId, true);
        return ResponseDTO.ok();
    }

    public ResponseDTO<CuttingInfoVO> sortDetail(Long cuttingId) {
        CuttingInfoEntity entity = cuttingInfoDao.selectById(cuttingId);
        if (entity == null){
            return ResponseDTO.error(UserErrorCode.DATA_EMPTY);
        }

        return ResponseDTO.ok(SmartBeanUtil.copy(entity, CuttingInfoVO.class));
    }
}
